//
//  UIView+SLExtension.m
//
//  Created by Alin on 15/12/31.
//  Copyright © 2015年 Alin. All rights reserved.
//

#import "UIView+SLExtension.h"
#import <objc/runtime.h>
#import "UIColor+CustomColor.h"
#import "UIView+aRLayer.h"
#import "MBProgressHUD.h"


CGFloat const aRound = 15.0f;

@implementation UIView (SLExtension)

- (void)setX:(CGFloat)x
{
    x = isnan(x)?0:x;
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame= frame;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (CGSize)size
{
    return self.frame.size;
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (BOOL)isShowingOnKeyWindow{
    // 主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    // 以主窗口左上角为坐标原点, 计算self的矩形框
    CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
    CGRect winBounds = keyWindow.bounds;
    
    // 主窗口的bounds 和 self的矩形框 是否有重叠
    BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
    
    return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;
}


#pragma mark - 添加一个string的tag属性
static void *TagStrKey = &TagStrKey;
- (NSString *)tagStr
{
    return objc_getAssociatedObject(self, TagStrKey);
}

- (void)setTagStr:(NSString *)tagStr
{
    objc_setAssociatedObject(self, TagStrKey, tagStr, OBJC_ASSOCIATION_COPY_NONATOMIC);
}


- (void)removeAllSubviews{
    if(!self.subviews) return;
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
}

/*
    custom AlertView
 */
- (UIView*)createAlertViewBy:(UIView*)customView size:(CGSize)size{
    UIWindow *keyWindow           = [UIApplication sharedApplication].keyWindow;
    UIView *backgroudView         = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    backgroudView.backgroundColor = [UIColor hexStringToColor:@"#000000" :0.6];
    [keyWindow addSubview:backgroudView];

    customView.size               = size;
    customView.center             = backgroudView.center;
    [customView btnCorner: aRound :0 :nil];
    [backgroudView addSubview:customView];
    return backgroudView;
}

+(void)showloginTip:(NSString*)str{
    if (!str) {
        str = @"no data";
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = NSLocalizedString(str, @"HUD message title");
    [hud hideAnimated:YES afterDelay:1.0f];
}


- (void)showMessageWithText:(NSString *)text {
    UILabel *alertLabel = [[UILabel alloc] init];
    alertLabel.font = [UIFont systemFontOfSize:15];
    alertLabel.text = text;
    alertLabel.textAlignment = NSTextAlignmentCenter;
    alertLabel.layer.masksToBounds = YES;
    alertLabel.textColor = [UIColor whiteColor];
    alertLabel.frame = CGRectMake(0, 0, 100, 80);
    [alertLabel sizeToFit];
    alertLabel.width += 50;
    alertLabel.height = 50;
    alertLabel.x = (self.width - alertLabel.width) * 0.5;
    alertLabel.y = (self.height - alertLabel.height) * 0.5;
    alertLabel.backgroundColor = [UIColor colorWithRed:25/255.0 green:25/255.0 blue:25/255.0 alpha:1.0];
    alertLabel.layer.cornerRadius = 10.0f;
    [[UIApplication sharedApplication].keyWindow addSubview:alertLabel];
    
    [UIView animateWithDuration:1.5 animations:^{
        alertLabel.alpha = 0.0;
    } completion:^(BOOL finished) {
        [alertLabel removeFromSuperview];
    }];
}

- (UIImage*) GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
    CGRect r= CGRectMake(0.0f, 0.0f, 1.0f, height);
    UIGraphicsBeginImageContext(r.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, r);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}

- (UIViewController*)superViewController
{
    for (UIView* next = [self superview]; next; next = next.superview)
    {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]])
        {
            [UIView class];
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

@end
